Repository navigation
Size sync requests by bytes, pace by CPU time, and sync off the main thread - #8
Merged
Merged
Conversation
…thread - Requests hold as many rows as fit in maxBatchBytes (default 4 MB for AWARE Micro, 900 KB for other servers), measured in the form the server receives. maxBatchRows caps the row count; batchSize is now the DB read chunk. - 413 halves the per-host limit and remembers it; a timeout halves it for the run and grows back; 429/503 wait for Retry-After. The same rows are resent. - The fixed 0.1 s wait between requests is replaced by a rest proportional to the CPU time of the last batch (cpuRestRatio, backgroundCpuRestRatio), longer in Low Power Mode and when the device is hot. - Sync work runs on a serial queue per table, never on the main thread, with a lower QoS in the background. - SQLiteEngine writes each upload row as JSON straight from the SQLite values (Engine.fetchUploadJSON), without a dictionary per row. Values are the same as before and REALs keep full precision (json_object() keeps only 15 digits). Load test (360,000 accelerometer rows, AWARE Micro-like server): sync time 55.6 s -> 15.8 s, requests 360 -> 23, UI response max during sync 36.9 ms -> 12.0 ms, peak phys_footprint 49 -> 56 MB, no rows lost; a server rejecting bodies over 1 MB costs two 413s, then every request fits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
A sync used to send 1,000 rows per request, wait a fixed 0.1 s between requests, and prepare requests on the main thread. This PR changes that:
maxBatchBytes, measured in the form the server receives. The default is 4 MB for AWARE Micro (Google App Engine accepts up to 32 MB) and 900 KB for other servers (below the nginx default of 1 MB and PHP'spost_max_size).maxBatchRows(20,000) caps the row count, andbatchSizeis now the database read chunk.UserDefaults. On a timeout, it is halved for the running app and grows back after 20 successful requests. On a 429 or 503, the sync waits forRetry-After. In all three cases the same rows are sent again, so nothing is lost.cpuRestRatiois 1 in the foreground andbackgroundCpuRestRatiois 3 in the background.batchIntervalsets a minimum rest; set it to0.1for the 1.6 behaviour..backgroundQoS while the app is in the background. Completion handlers are still called on the main thread.SQLiteEnginewrites each row as JSON straight from the SQLite values (Engine.fetchUploadJSON), without making a dictionary per row first.json_object()is not used because it keeps only 15 significant digits.getUploadCandidatesmust also overridegetUploadCandidateJSON; returningnilis enough.Results
Load test: 360,000 accelerometer rows in the iOS Simulator, sent to a local AWARE Micro-like server.
Tests
All 105 tests pass. This PR adds:
DbSyncBatchingTests: size limits for JSON and form bodies, compact format and body handler,maxBatchRows, an oversized row, the 413 and timeout limits, pacing, and config defaults.UploadJSONTests: on a real SQLite database, checks that the JSON rows carry the same values asfetch, including NULL columns, string escapes, Unicode, and doubles compared bit for bit, and that a BLOB row is marked. A full sync sends every row once with exact numbers.🤖 Generated with Claude Code